package com.hdax.gulimall.ware.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.hdax.common.exception.BizCodeEnume;
import com.hdax.common.to.mq.StockLockTo;
import com.hdax.gulimall.ware.exception.NoStockExeption;
import com.hdax.gulimall.ware.vo.LockStockResult;
import com.hdax.gulimall.ware.vo.SkuHasStockVo;
import com.hdax.gulimall.ware.vo.WareSkuLockVo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.hdax.gulimall.ware.entity.WareSkuEntity;
import com.hdax.gulimall.ware.service.WareSkuService;
import com.hdax.common.utils.PageUtils;
import com.hdax.common.utils.R;



/**
 * 商品库存
 *
 * @author xiaohou
 * @email 2687781461@qq.com
 * @date 2023-04-08 23:42:59
 */
@RestController
@RequestMapping("ware/waresku")
public class WareSkuController {
    @Autowired
    private WareSkuService wareSkuService;

    @Autowired
    private RabbitTemplate rabbitTemplate;



    @GetMapping("/haha")
    public void haha(){
        StockLockTo stockLockTo = new StockLockTo();
        rabbitTemplate.convertAndSend("stock-event-exchange","stock.locked",stockLockTo);
    }
    /**
     * 库存的锁定(订单号 商品的数量id)
     * @param wareSkuLockVo
     * @return
     */
    @PostMapping("/lock/order")
    public R orderLockStock(@RequestBody WareSkuLockVo wareSkuLockVo){
       try {
           wareSkuService.orderLockStock(wareSkuLockVo);
           return R.ok();
       }catch (NoStockExeption e){
           return R.error(BizCodeEnume.NO_STOCK_EXCEPTION.getCode(), BizCodeEnume.NO_STOCK_EXCEPTION.getMsg());
       }
    }



    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("ware:waresku:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = wareSkuService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 查看sku是否有库存
     */

    @PostMapping("/hasStock")
    public R<List<SkuHasStockVo>> getSkuHasStock(@RequestBody List<Long> skuIds){
       List<SkuHasStockVo> list= wareSkuService.getSkuHasStock(skuIds);
        R<List<SkuHasStockVo>> ok = R.ok();
        ok.setData(list);
        return ok;
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("ware:waresku:info")
    public R info(@PathVariable("id") Long id){
		WareSkuEntity wareSku = wareSkuService.getById(id);

        return R.ok().put("wareSku", wareSku);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("ware:waresku:save")
    public R save(@RequestBody WareSkuEntity wareSku){
		wareSkuService.save(wareSku);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("ware:waresku:update")
    public R update(@RequestBody WareSkuEntity wareSku){
		wareSkuService.updateById(wareSku);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("ware:waresku:delete")
    public R delete(@RequestBody Long[] ids){
		wareSkuService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
